using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
 
namespace CategoryProductPicker
{
    public partial class CategoryPicker : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        public string categoryId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                if (categoryDDL.SelectedValue.ToString() != "-1")
                {
                    categoryId = categoryDDL.SelectedValue.ToString();
                }
                else
                {
                    categoryId = "";
                }
            }
            else
            {
                Commerce.Common.CategoryCollection categories = null;

                categories = new CategoryCollection().OrderByAsc("CategoryName").Load();

                categoryDDL.Items.Clear();

                ListItem initialitem = new ListItem();
                initialitem.Text = "--- Select Category ---";
                initialitem.Value = "-1";
                categoryDDL.Items.Add(initialitem);

                foreach (Category thisCat in categories)
                {
                    ListItem item = new ListItem();
                    item.Text = thisCat.CategoryName;
                    item.Value = thisCat.CategoryID.ToString();
                    if (categoryId == thisCat.CategoryID.ToString())
                    {
                        item.Selected = true;
                    }
                    categoryDDL.Items.Add(item);
                }
            }
        }

        protected void categoryDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (categoryDDL.SelectedValue.ToString() != "-1")
            {
                categoryId = categoryDDL.SelectedValue.ToString();
            }
            else
            {
                categoryId = "";
            }
        }

        #region IUsercontrolDataEditor Members

        public object value
        {
            get
            {
                return categoryId;
            }
            set
            {
                categoryId = value.ToString();
            }
        }

        #endregion

    }
}